/*
 * @Author: HX
 * @Date: 2024-12-07 14:09:13
 * @LastEditors: HX
 * @LastEditTime: 2024-12-16 15:24:39
 */
import global from '../core/global.js';
import { vector3Offset } from '../core/common.js';
import { jsonToCar3FromDegrees } from '../core/common.js';
import { Polyline3D } from './polyline.js';

let Animation = {
	add(options, cb) {
		let points = new SSmap.PointVector();
		let trackAnim = new SSmap.TrackAnimation();
		if (options.geoJsonUrl) {
			trackAnim.geoJson = options.geoJsonUrl;
		}
		trackAnim.model = options.url;
		if (options.positions) {
			options.positions.forEach((position) => {
				let _position = jsonToCar3FromDegrees(position);
				points.push_back(_position.toVector3());
			});
			trackAnim.points = points;
		}
		trackAnim.speed = options.speed != undefined ? options.speed : 1; //速度
		trackAnim.alongTheWay = options.alongTheWay || false;
		trackAnim.clampedTerrain = options.clampedTerrain || false;
		trackAnim.helicopterMode = options.helicopterMode || true;
		if (options.scale) {
			trackAnim.scale = SSmap.Vector3.create(options.scale[0], options.scale[1], options.scale[2]); //比例
		}
		if (options.rotatex) {
			trackAnim.setRotateX(options.rotatex || 0); //x轴旋转
		}
		if (options.rotatey) {
			trackAnim.setRotateY(options.rotatey || 0); //x轴旋转
		}
		if (options.rotatez) {
			trackAnim.setRotateZ(options.rotatez || 0); //x轴旋转
		}
		trackAnim.geometryError = 0; //0-12, 0 不对模型做裁切优化, 很远依然能看到
		trackAnim.componentComplete();
		let entity = new SSmap.Entity();
		entity.addComponent(trackAnim);
		points.delete(); //使用完需要删除
		// Polyline3D.add({ points: options.positions, style: { mode: 'Absolute' } }) //轨迹线
		let id = global.AnimationStore.add(trackAnim, options.id);
		trackAnim.readyPromise.then(() => {
			if (options.id) {
				let animEntity = trackAnim.animEntity();
				animEntity.tag = options.id;
			}
			cb && cb(id);
		});
		return id;
	},
	_getAnimation(id) {
		let trackAnim = global.AnimationStore.get(id);
		return trackAnim;
	},
	flyTo(options, cb = () => {}) {
		let trackAnim = global.AnimationStore.get(options.id);
		if (!trackAnim) return;
		trackAnim = trackAnim.animEntity();
		let scene = window.viewer.scene;
		let camera = scene.mainCamera;
		let cameraController = camera.cameraController();
		let stack = [];
		let renderer = null;
		stack.push(trackAnim.entity ? trackAnim.entity : trackAnim);
		while (stack.length) {
			let entity = stack.pop();
			if (entity.renderer) {
				renderer = entity.renderer;
				break;
			}
			let children = entity.childEntities;
			for (let i = 0; i < children.size(); i++) {
				stack.push(children.get(i));
			}
		}
		if (renderer) {
			let center = renderer.boundingVolume.center;
			let radius = renderer.boundingVolume.boundingSphere.radius * 2 + 5;
			let carto = center.toCartographic();
			carto.height = Math.abs(carto.height + radius);
			let position = carto.toVector3();
			position = vector3Offset(position, {
				offsetY: -radius,
			});
			cameraController.flyTo(position, 2, 0, -45, 0);
			cb(true);
		} else {
			cb(false);
		}
	},
	_getBoundingVolume(options) {
		let trackAnim = global.AnimationStore.get(options.id);
		if (!trackAnim) return;
		trackAnim = trackAnim.animEntity();
		let stack = [];
		let renderer = null;
		stack.push(trackAnim.entity ? trackAnim.entity : trackAnim);
		while (stack.length) {
			let entity = stack.pop();
			if (entity.renderer) {
				renderer = entity.renderer;
				break;
			}
			let children = entity.childEntities;
			for (let i = 0; i < children.size(); i++) {
				stack.push(children.get(i));
			}
		}
		return renderer.boundingVolume;
	},
	remove(id) {
		global.AnimationStore.has(id) && global.AnimationStore.remove(id);
	},
	removeAll() {
		global.AnimationStore.removeAll();
	},
};
export { Animation };
